package ro.fortech.peaa.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import ro.fortech.peaa.domain.Department;
import ro.fortech.peaa.domain.Employee;
import ro.fortech.peaa.service.impl.DepartmentManager;
import ro.fortech.peaa.service.impl.EmployeeManager;

/**
 * @author andrei
 *
 */
public class DepartmentController extends AbstractController {
	
	private DepartmentManager departmentManager;
	private EmployeeManager employeeManager;

	/**
	 * @param employeeManager the employeeManager to set
	 */
	public void setEmployeeManager(EmployeeManager employeeManager) {
		this.employeeManager = employeeManager;
	}

	/**
	 * @param departmentManager the departmentManager to set
	 */
	public void setDepartmentManager(DepartmentManager departmentManager) {
		this.departmentManager = departmentManager;
	}
	
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		List<Department> departments = departmentManager.findAll();
		List<Employee> employees = employeeManager.findAll();
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("departments", departments);
		Map<Integer, List<Double>> depMap = new HashMap<Integer, List<Double>>();
		for (Employee employee : employees) {
			Integer depId = employee.getDepartmentId();
			Double salary = employee.getSalary();
			List<Double> depSalaries = depMap.get(depId);
			if (depSalaries == null) {
				depSalaries = new ArrayList<Double>();
				depMap.put(depId, depSalaries);
			}
			depSalaries.add(salary);
		}
		modelMap.put("depMap", depMap);
		
		return new ModelAndView("department_list", modelMap);
	}
}
